﻿Public Class InitData
    Public WithEvents SBO_Application As SAPbouiCOM.Application
    Public Sub InitData()
        'Create/Update table
        InitTable()
        'Create/Update sp
        InitSP()
        'Create/Update UDT
        InitUDT()
        'Create/Update UDF
        InitUDF()
        'Create/Update UDO
        InitUDO()
    End Sub
    Private Sub InitTable()

    End Sub
    Private Sub InitSP()

    End Sub
    Private Sub InitUDT()
        'P_MENU
        'P_MENUDET
        Dim ret As String = ""
        ret = CreateUDT("P_MENU", "POS MENU", SAPbobsCOM.BoUTBTableType.bott_MasterData)
        SBO_Application.SetStatusBarMessage("UDT_P_MENU: " + IIf(ret = "", "OK", ret), , False)
        If ret = "" Then
            ReconnectSBO()
            ret = CreateUDT("P_MENUDET", "POS MENU DETAIL", SAPbobsCOM.BoUTBTableType.bott_MasterDataLines)
            SBO_Application.SetStatusBarMessage("UDT_P_MENUDETAIL: " + IIf(ret = "", "OK", ret), , False)
        End If
    End Sub
    Private Function InitUDF() As String
        'P_MENUDET
        'ITEMCODE, ITEMNAME
        Try
            ReconnectSBO()
            Dim ret As String
            ret = CreateUDF("@P_MENUDET", "ItemCode", "Item Code", SAPbobsCOM.BoFieldTypes.db_Alpha, SAPbobsCOM.BoFldSubTypes.st_None, 30)
            SBO_Application.SetStatusBarMessage("UDF_ItemCode_P_MENUDET: " + IIf(ret = "", "OK", ret), , False)
            If ret = "" Then
                ReconnectSBO()
                ret = CreateUDF("@P_MENUDET", "ItemName", "Item Name", SAPbobsCOM.BoFieldTypes.db_Alpha, SAPbobsCOM.BoFldSubTypes.st_None, 100)
                SBO_Application.SetStatusBarMessage("UDF_ItemName_P_MENUDET: " + IIf(ret = "", "OK", ret), , False)
            End If

            Return ret
        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
    Private Sub InitUDO()
        'P_MENUSETUP
        Dim ChildTableList As IList(Of String) = New List(Of String)
        ChildTableList.Add("P_MENUDET")
        Dim ret As String = ""
        ReconnectSBO()
        ret = RegisterUDO("P_MENU", "POS MENU", SAPbobsCOM.BoUDOObjType.boud_MasterData, "P_MENU", ChildTableList, SAPbobsCOM.BoYesNoEnum.tNO, SAPbobsCOM.BoYesNoEnum.tNO, SAPbobsCOM.BoYesNoEnum.tYES, SAPbobsCOM.BoYesNoEnum.tNO)
        SBO_Application.SetStatusBarMessage("UDO_P_MENU: " + IIf(ret = "", "OK", ret), , False)
    End Sub

    Private Function CreateUDT(ByVal tableName As String, ByVal tableDesc As String, ByVal tableType As SAPbobsCOM.BoUTBTableType) As String
        Dim ret As String = ""
        Dim oUdtMD As SAPbobsCOM.UserTablesMD = Nothing
        Try
            GC.Collect()
            oUdtMD = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserTables)
            Dim lRetCode As Long
            If oUdtMD.GetByKey(tableName) = False Then
                oUdtMD.TableName = tableName
                oUdtMD.TableDescription = tableDesc
                oUdtMD.TableType = tableType
                lRetCode = oUdtMD.Add
            Else
                'lRetCode = oUdtMD.Update
            End If
            If lRetCode <> 0 Then
                Dim sErrMsg As String = ""
                PublicVariable.oCompany.GetLastError(lRetCode, sErrMsg)
                ret = sErrMsg
            End If

            'important:release MD object right after finishing.
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oUdtMD)
            oUdtMD = Nothing
            GC.Collect()
        Catch ex As Exception
            ret = ex.ToString
        End Try

        Return ret
    End Function

    Private Function CreateUDF(ByVal tableName As String, ByVal fieldName As String, ByVal desc As String, _
                               ByVal fieldType As SAPbobsCOM.BoFieldTypes, ByVal subType As SAPbobsCOM.BoFldSubTypes, _
                               ByVal editSize As Integer) As String
        Try
            Dim IsUDT As Boolean = False
            If tableName.Contains("@") Then
                IsUDT = True
            End If
            'tableName = tableName.Replace("@", "")
            Dim ors As SAPbobsCOM.Recordset
            ors = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            ors.DoQuery("SELECT FieldID FROM CUFD WHERE TableID = '" + tableName + "' AND AliasID = N'" + fieldName + "'")

            Dim fieldID As Long = -1
            If ors.RecordCount > 0 Then
                fieldID = ors.Fields.Item("FieldID").Value
            End If
            System.Runtime.InteropServices.Marshal.ReleaseComObject(ors)
            ors = Nothing
            GC.Collect()

            Dim oUdfMD As SAPbobsCOM.UserFieldsMD = Nothing
            oUdfMD = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields)

            GC.Collect()
            oUdfMD.TableName = tableName
            oUdfMD.Name = fieldName
            oUdfMD.Type = fieldType
            oUdfMD.Description = desc
            oUdfMD.SubType = subType
            oUdfMD.EditSize = editSize

            Dim lRetCode As Integer = 0

            If oUdfMD.GetByKey(tableName, fieldID) = True Then
                'lRetCode = oUdfMD.Update()
            Else
                lRetCode = oUdfMD.Add()
            End If

            System.Runtime.InteropServices.Marshal.ReleaseComObject(oUdfMD)
            oUdfMD = Nothing
            GC.Collect()

            If lRetCode <> 0 Then
                Dim sErrMsg As String = ""
                PublicVariable.oCompany.GetLastError(lRetCode, sErrMsg)
                Return sErrMsg
            Else
                Return ""
            End If
        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function

    Private Function RegisterUDO(ByVal udoCode As String, ByVal udoName As String, ByVal udoType As SAPbobsCOM.BoUDOObjType, ByVal mainTable As String, _
                    ByVal childTableList As IList(Of String), ByVal canCalcel As SAPbobsCOM.BoYesNoEnum, ByVal canColse As SAPbobsCOM.BoYesNoEnum, _
                    ByVal canDelete As SAPbobsCOM.BoYesNoEnum, ByVal manageSeries As SAPbobsCOM.BoYesNoEnum) As String
        Try
            Dim oUdoMD As SAPbobsCOM.UserObjectsMD = Nothing
            Try
                'System.Runtime.InteropServices.Marshal.ReleaseComObject(oUdoMD)
                'oUdoMD = Nothing
                GC.Collect()
            Catch ex As Exception

            End Try

            oUdoMD = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserObjectsMD)
            Dim udoExisted As Boolean = False
            udoExisted = oUdoMD.GetByKey(udoCode)
            oUdoMD.Code = udoCode
            oUdoMD.Name = udoName
            oUdoMD.ObjectType = udoType
            oUdoMD.TableName = mainTable 'Main user table (same type as the UDO)
            If Not (childTableList Is Nothing) And childTableList.Count > 0 Then
                Dim ChildTables As SAPbobsCOM.UserObjectMD_ChildTables = oUdoMD.ChildTables
                For Each child As String In childTableList
                    ChildTables.Add()
                    ChildTables.TableName = child
                Next
            End If

            oUdoMD.CanCancel = canCalcel
            oUdoMD.CanClose = canColse
            oUdoMD.CanDelete = canDelete
            oUdoMD.ManageSeries = manageSeries
            oUdoMD.UseUniqueFormType = SAPbobsCOM.BoYesNoEnum.tYES
            oUdoMD.EnableEnhancedForm = SAPbobsCOM.BoYesNoEnum.tYES
            oUdoMD.CanCreateDefaultForm = SAPbobsCOM.BoYesNoEnum.tYES
            oUdoMD.MenuItem = SAPbobsCOM.BoYesNoEnum.tYES
            oUdoMD.MenuCaption = udoName
            oUdoMD.MenuUID = udoCode
            oUdoMD.FatherMenuID = 2048
            oUdoMD.Position = 0

            If udoType = SAPbobsCOM.BoUDOObjType.boud_MasterData Then
                oUdoMD.FormColumns.FormColumnAlias = "Code"
                oUdoMD.FormColumns.FormColumnDescription = "Code"
                oUdoMD.FormColumns.Add()
            End If
            If udoType = SAPbobsCOM.BoUDOObjType.boud_Document Then
                oUdoMD.FormColumns.FormColumnAlias = "DocEntry"
                oUdoMD.FormColumns.FormColumnDescription = "DocEntry"
                oUdoMD.FormSRF = "DocEntry"
                oUdoMD.FormColumns.Add()
            End If


            Dim lRetCode As Long
            If udoExisted Then
                'lRetCode = oUdoMD.Update
            Else
                lRetCode = oUdoMD.Add
            End If

            If lRetCode <> 0 Then
                Dim sErrMsg As String = ""
                PublicVariable.oCompany.GetLastError(lRetCode, sErrMsg)
                Return sErrMsg
            End If
            ' End If
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oUdoMD)
            oUdoMD = Nothing
            GC.Collect()

            Return ""
        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
    Private Sub ReconnectSBO()
        'Dim sCookie As String
        'Dim sConnectionContext As String
        'Try
        '    PublicVariable.oCompany = New SAPbobsCOM.Company
        '    sCookie = PublicVariable.oCompany.GetContextCookie
        '    sConnectionContext = SBO_Application.Company.GetConnectionContext(sCookie)
        '    If PublicVariable.oCompany.Connected = True Then
        '        PublicVariable.oCompany.Disconnect()
        '    End If
        '    PublicVariable.oCompany.SetSboLoginContext(sConnectionContext)
        'Catch ex As Exception
        '    SBO_Application.SetStatusBarMessage(ex.ToString, , False)
        'End Try

    End Sub
End Class
